package com.luoxue.execsql.rest.bean.request.impl;

import com.luoxue.execsql.common.util.ResultBody;
import com.luoxue.execsql.rest.bean.request.IRequestExtension;
import com.luoxue.execsql.rest.entity.User;
import com.luoxue.execsql.rest.entity.UserTest;
import com.luoxue.execsql.rest.uitl.GsonUtil;
import com.luoxue.execsql.rest.uitl.HttpHolder;
import org.apache.logging.log4j.util.Strings;
import org.jasypt.encryption.StringEncryptor;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Map;
import java.util.function.Supplier;

public class IRequestExtensionDefault implements IRequestExtension {
    private final String userTag = "user";

    @Resource
    private StringEncryptor stringEncryptor;

    @Override
    public Mono<ResultBody> addParam(Map<String, Object> param, Supplier<ResultBody> next) {
        return HttpHolder.getRequest().map(request -> {
            // 测试用户 52G9dsQo093j4wvf85x4z6xLVsWBFjLFOfDikWdTuXrwJ+/fQkEFOde5HqTHGaFS9Y2O0q1EqpLfxUfGdqBxA8k3MwGzk8ty22Bt9UUbiXnbTei6F0ar1lmKfo8CrcJccrEADiOAsXe0Qr+mL1MIbw==
            String userStr = request.getHeaders().getFirst(userTag);
            if (Strings.isNotBlank(userStr)) {
                String decrypt = stringEncryptor.decrypt(userStr);
                User user = GsonUtil.createGson().fromJson(decrypt, User.class);
                param.put(userTag, user);
            }
            return next.get();
        });
    }
}
